package com.qiye.livevideo.controller;

import com.qiye.livevideo.common.CodeMsg;
import com.qiye.livevideo.common.ResultInfo;
import com.qiye.livevideo.model.User;
import com.qiye.livevideo.service.UserService;
import com.qiye.livevideo.utils.AliMessageUtils;
import com.qiye.livevideo.utils.AuthCodeUtil;
import com.qiye.livevideo.utils.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 用户表 controller
 * @author tongling
 */
@Api(description = "用户表")
@Controller
@RequestMapping(value = "/user")
public class UserController {


    @Resource
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/sendSms", method = RequestMethod.POST)
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    public ResultInfo deleteByPrimaryKey(@RequestParam(value = "phone", required = true) String phone,
                                         HttpServletRequest request) {
        try {
            String code = AuthCodeUtil.getRandomStr(6, 0);
            AliMessageUtils.sendSmsByTemplate(phone,code,null);
            request.getSession().setAttribute("code", code);
            request.getSession().setMaxInactiveInterval(60*5);
            return ResultInfo.success(CodeMsg.SUCCESS, "发送成功");
        } catch (Exception e) {
            return ResultInfo.error(CodeMsg.ERROR, "发送失败");
        }
    }

    /**
     * 绑定手机号
     */
    @ResponseBody
    @RequestMapping(value = "/bindingPhone", method = RequestMethod.POST)
    @ApiOperation(value = "绑定手机号", notes = "根据主键id,绑定手机号")
    public ResultInfo deleteByPrimaryKey(@RequestParam(value = "phone", required = true) String phone,
                                         @RequestParam(value = "code", required = true) String code,
                                         @RequestParam(value = "id", required = true) String id,
                                         HttpServletRequest request) {
        String cod = "code";
        if (EmptyUtils.isNotEmpty(code)) {
            if(EmptyUtils.isNotEmpty(request.getSession().getAttribute(cod))){
                if (request.getSession().getAttribute(cod).toString().equals(code)) {
                    User user = new User();
                    user.setId(id);
                    user.setPhone(phone);
                    return ResultInfo.success(userService.update(user));
                } else {
                    return ResultInfo.error(CodeMsg.ERROR, "验证码不正确");
                }
            }else{
                return ResultInfo.error(CodeMsg.ERROR, "验证码已失效");
            }
        }
        return ResultInfo.error(CodeMsg.ERROR, "请输入验证码");
    }


    /**
     * 删除-用户表
     */
    @ResponseBody
    @RequestMapping("/deleteByPrimaryKey")
    @ApiOperation(value = "根据主键删除", notes = "参数只用到了主键id,其他参数忽略", httpMethod = "POST")
    public ResultInfo deleteByPrimaryKey(Integer id, HttpServletRequest request, HttpServletResponse response) {
        return ResultInfo.success(userService.deleteByPrimaryKey(id));
    }


    /**
     * 保存 (主键为空则增加否则修改)-> 用户表
     */
    @ResponseBody
    @RequestMapping("/save")
    @ApiOperation(value = "保存", notes = "主键为空则增加否则修改", httpMethod = "POST")
    public ResultInfo save(User user, HttpServletRequest request, HttpServletResponse response) {
        if (user.getId() == null) {
            return ResultInfo.success(userService.insert(user));
        }
        return ResultInfo.success(userService.update(user));

    }


    /**
     * 根据主键查询->用户表
     */
    @ResponseBody
    @RequestMapping("/selectByPrimaryKey")
    @ApiOperation(value = "根据主键查询", notes = "参数只用到了主键.", httpMethod = "POST")
    public ResultInfo selectByPrimaryKey(Integer id, HttpServletRequest request, HttpServletResponse response) {
        return ResultInfo.success(userService.selectByPrimaryKey(id));
    }


    /**
     * 根据条件查询(所有的实体属性都是条件,如果为空则忽略该字段)->用户表
     */
    @ResponseBody
    @RequestMapping("/selectByCondition")
    @ApiOperation(value = "根据条件查询", notes = "参数为空则忽略.", httpMethod = "POST")
    public ResultInfo selectByCondition(User user, HttpServletRequest request, HttpServletResponse response) {
        return ResultInfo.success(userService.selectByCondition(user));
    }


    /**
     * 分页查询 (所有的实体属性都是条件,如果为空则忽略该字段) ( 默认 page=1 pageSize=10)->用户表
     */
    @ResponseBody
    @RequestMapping("/selectAllByPaging")
    @ApiOperation(value = "分页查询", notes = "默认page=1pageSize等于10", httpMethod = "POST")
    public ResultInfo selectAllByPaging(User user, HttpServletRequest request, HttpServletResponse response) {
        return ResultInfo.success(userService.selectAllByPaging(user));
    }


}
